<template>
	<view class="apply">
		<cu-custom bgColor="bg-gradual-orange" :isBack="true">
			<block slot="content">商家入驻申请</block>
		</cu-custom>
		<view class="cu-form-group no-border">
			<view class="title">商家名称</view>
			<input v-model="form.title" placeholder="请输入商家名称"></input>
		</view>
		<!-- <view class="cate-choose cu-form-group no-border">
			<view class="title">入驻类型</view>
			<input v-model="type.name" @tap="changeType" disabled="disabled" placeholder="请选择入驻分类"></input>
			<text class="cuIcon-question" @click="checkExplain"></text>
		</view> -->
		<view class="cu-form-group no-border" v-show="true">
			<view class="title">商家UID</view>
			<input v-model="form.bind_uid" placeholder="请输入商家UID"></input>
		</view>
		<view class="tips" v-show="false">(可以为空，拓店员代商家入驻的时候必填)</view>

		<view class="cu-form-group no-border" v-show="false">
			<view class="title">拓店员UID</view>
			<input v-model="form.agent_uid" placeholder="请输入拓店员UID"></input>
		</view>

		<view class="group-bg"></view>
		<view class="cu-form-group">
			<view class="title">店招logo:(建议上传750*530或横向图片)</view>
		</view>
		<view class="cu-form-group no-border">
			<view class="file">
				<view class="item">
					<text class='cuIcon-close' @tap="DelImg('logo')" v-if="form.logo"></text>
					<text class='cuIcon-add' @tap="ChooseImage('logo')" v-if="!form.logo"></text>
					<image class="img" :src="form.logo" mode="aspectFill" @tap="ViewImage('logo')" v-if="form.logo">
					</image>
					<text class='label' style="height: 10px;"></text>
				</view>
			</view>
		</view>

		<!-- <view class="group-bg"></view> -->
		<view class="cu-form-group">
			<view class="title">店内照片</view>
		</view>
		<view class="cu-form-group no-border">
			<view class="file">
				<view class="item">
					<text class='cuIcon-close' @tap="DelImg('indoor_pic')" v-if="form.indoor_pic"></text>
					<text class='cuIcon-add' @tap="ChooseImage('indoor_pic')" v-if="!form.indoor_pic"></text>
					<image class="img" :src="form.indoor_pic" mode="aspectFill" @tap="ViewImage('indoor_pic')"
						v-if="form.indoor_pic"></image>
					<text class='label' style="height: 10px;"></text>
				</view>
			</view>
		</view>

		<view class="group-bg"></view>
		<view class="cu-form-group">
			<view class="title">商品相册</view>
			<view class="action">{{imgList.length}}/8</view>
		</view>
		<view class="my-img-upload-group">
			<my-img-upload-group num="8" v-model="imgList"></my-img-upload-group>
		</view>

		<view class="group-bg"></view>
		<view class="cu-form-group" v-if="typeChoose">
			<view class="title">折扣比例</view>
			<input v-model="form.discount" placeholder="例：7折输入70" maxlength="10"></input>
			<text class='text-black'>%</text>
		</view>

		<!-- #ifndef H5 || APP-PLUS || MP-ALIPAY -->
		<view class="cu-form-group">
			<view class="title">所在地区</view>
			<picker mode="region" :disabled="disabled" @change="RegionChange" v-model="form.region">
				<view class="picker" style="text-align: left;">
					{{form.region[0]}}，{{form.region[1]}}，{{form.region[2]}}
				</view>
			</picker>
		</view>
		<!-- #endif -->
		<view class="cu-form-group">
			<view class="title">地址选择</view>
			<input v-model="form.address" :disabled="true" placeholder="请选择地址" @tap="ChooseLocation"></input>
			<text class='cuIcon-locationfill text-orange'></text>
		</view>

		<view class="cu-form-group">
			<view class="title">联系方式</view>
			<input v-model="form.mobile" placeholder="请输入联系方式" maxlength="11"></input>
		</view>

		<view class="cu-form-group no-border">
			<textarea @input="textareaAInput" maxlength="200" placeholder="申请备注" :value="form.reason"></textarea>
		</view>
		<view class="group-bg"></view>

		<view class="padding flex flex-direction">
			<button class="cu-btn bg-gradual-orange margin-tb-sm lg" :loading="loading" @tap="submit">提交</button>
		</view>

		<view class="cu-modal" style="z-index: 99;" :class="modalActive ? 'show' : ''">
			<view class="cu-dialog">
				<view class="cu-bar bg-white justify-end">
					<view class="content">提示</view>
					<view class="action" @tap="hideModal">
						<text class="cuIcon-close text-red"></text>
					</view>
				</view>
				<view class="padding-sm">
					<view class="moda-msg">{{message}}</view>
					<radio-group @change="radioChange">
						<label class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in items" :key="item.value">
							<view class="uni-list-cell-item">
								<radio :value="item.value" :checked="index === current" />
								<text>{{item.name}}</text>
							</view>
						</label>
					</radio-group>
				</view>
				<view class="cu-bar bg-white">
					<view class="action margin-0 flex-sub text-green solid-left" @tap="hideModal">取消</view>
					<view class="action margin-0 flex-sub  solid-left" @tap="selectWay">确定</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import {
		upload
	} from "@/common/upload.js";
	import shop from '@/api/shop';
	import MyImgUploadGroup from '@/components/MyImgUploadGroup.vue';
	import qqMap from '@/plugins/qqmap-wx-jssdk.min.js';
	export default {
		data() {
			return {
				form: {
					title: '',
					logo: '',
					bind_uid: '',
					agent_uid: '',
					discount: '',
					reason: '',
					region: ['北京市', '北京市', '东城区'],
					indoor_pic: '',
					address: '',
					lng: '',
					lat: '',
					albumInfo: '',
					mobile: '',
					type: '',
					rebate: '',
					share_man: 0,
					position: 'openman'
				},
				imgList: [],
				loading: false,
				// type:{name:'请选择入驻分类'},
				typeList: [],
				itemList: [],
				typeChoose: false,
				modalActive: false,
				title: '',
				items: [{
					name: '自动审核通道(推荐)',
					value: 1
				}, {
					name: '人工审核通道',
					value: 0
				}],
				current: 0,
				auto: false,
				message: ''
			}
		},
		computed: {
			...mapState(['userLocation', 'userInfo']),
		},
		components: {
			MyImgUploadGroup
		},
		onLoad(option) {
			if (option && option.outPutUid) {
				console.log(option.outPutUid);
				this.form.share_man = option.outPutUid
			}
			// this.form.bind_uid = this.userInfo.id
			this.loadInfo();
			this.getTypes();
		},
		methods: {
			async getTypes() {
				let res = await shop.getType({});
				if (res.code == 1) {
					// this.type = res.data[0];
					this.typeList = res.data;
					// this.form.rebate = this.type.id;
					let itemList = [];
					for (let i = 0; i < this.typeList.length; i++) {
						itemList.push(this.typeList[i].name);
					}
					this.itemList = itemList;
				} else {
					console.log(res);
				}
			},
			checkExplain() {
				uni.navigateTo({
					url: '../shop/agreement',
				});
			},
			changeType() {
				if (this.itemList.length < 1) {
					return false;
				}
				let itemList = this.itemList;
				let _this = this;
				uni.showActionSheet({
					itemList: itemList,
					success: function(res) {
						console.log('选中了第' + (res.tapIndex + 1) + '个按钮');
						_this.form.rebate = _this.typeList[res.tapIndex].id;
						_this.type = _this.typeList[res.tapIndex];
						if (_this.type.name == '智慧经营') {
							_this.typeChoose = true;
						} else {
							_this.typeChoose = false;
						}
					},
					fail: function(res) {}
				});
			},
			async loadInfo() {
				await this.$store.dispatch('getLocation');
				this.form.lng = this.userLocation.longitude;
				this.form.lat = this.userLocation.latitude;

				
			},
			
			async hideModal() {
				this.modalActive = false
			},
			radioChange(evt) {
				for (let i = 0; i < this.items.length; i++) {
					if (this.items[i].value == evt.target.value) {
						this.current = i;
						break;
					}
				}
			},
			selectWay() {
				const that = this;
				this.form.type = this.items[this.current].value;
				// 处理albumInfo字段 多个完整的URL地址以英文逗号,隔开
				this.form.albumInfo = this.imgList.join(",");
				shop.apply(this.form).then(res => {
					this.hideModal()
					if (res.code == 1) {
						// this.jumpTo(res.data.url, 'web')
						uni.navigateTo({
							url: '/pages/shipment/index?uid=' + res.data.uid,
						});
					} else {
						this.toast(res.msg);
					}
				})
			},
			async submit() {
				this.showModal({
					msg: ''
				})
			},
			showModal(res) {
				this.message = res.msg;
				this.auto = res.msg.indexOf('公安') > -1 ? true : false;
				this.current = 0;
				this.modalActive = true;
			},
			ChooseImage(type) {
				if (this.loading) {
					return;
				}
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (chooseRes) => {
						upload(chooseRes.tempFilePaths[0]).then(res => {
							this.form[type] = res.url;
						})
					}
				});
			},
			ViewImage(type) {
				const imgs = [];
				imgs.push(this.form[type]);
				uni.previewImage({
					urls: imgs,
					current: 0
				});
			},
			DelImg(type) {
				if (this.loading) {
					return;
				}
				uni.showModal({
					title: '删除图片',
					content: '确定要删除吗？',
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							this.form[type] = ''
						}
					}
				})
			},
			textareaAInput(e) {
				if (this.loading) {
					return;
				}
				this.form.reason = e.detail.value
			},
			textareaAddressAInput(e) {
				if (this.loading) {
					return;
				}
				this.form.address = e.detail.value
			},
			RegionChange(e) {
				if (this.loading || this.disabled) {
					return;
				}
				this.form.region = e.detail.value;
			},
			ChooseLocation() {
				if (this.loading || this.disabled) {
					return;
				}
				uni.chooseLocation({
					latitude: this.form.lat > 0 ? this.form.lat : '',
					longitude: this.form.lng > 0 ? this.form.lng : '',
					success: (res) => {
						if (!res.name) {
							this.form.address = res.address + res.name
							this.form.lat = res.latitude
							this.form.lng = res.longitude
							this.toast('请选择详细位置');
						} else {
							this.form.address = res.name;
							this.form.lat = res.latitude;
							this.form.lng = res.longitude;
							this.getOnlineDistrict(res.latitude, res.longitude);
						}
					}
				});
			},
			//这里调用腾讯地图api 获取用户位置所在区
			getOnlineDistrict(latitude, longitude) {
				const that = this;
				const map = new qqMap({
					key: 'NGCBZ-AKE6Q-EFH5N-BAIQA-CP3CT-4EF6U'
				})
				//通过这个方法来实现经纬度反推省市区
				map.reverseGeocoder({
					location: {
						latitude: latitude,
						longitude: longitude
					},
					success: function(res) {
						if (res.result && res.result.address_component) {
							const region = [
								res.result.address_component.province,
								res.result.address_component.city,
								res.result.address_component.district
							];
							if (region.length > 0) {
								that.form.region = region;
							}
						}
					},
					fail: function(error) {
						console.log(error)
					}
				})
			}
		}
	}
</script>

<style lang="less">
	.apply {
		background: #fff;

		.cu-btn[disabled] {
			color: #FFFFFF !important;
		}
	}

	.tips {
		background-color: #FFFFFF;
		color: red;
		margin: -15px 10px 0 10px;
		padding: 5px 15px;
		border-bottom: 1px solid #D8D8D8;
		font-size: 12px;
	}

	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}

	.my-img-upload-group {
		padding-top: 10px;

		.cu-form-group {
			border: none;
		}
	}

	.cate-choose input {
		padding-right: 60rpx;
	}

	.cate-choose .cuIcon-question {
		color: red;
		font-size: 40rpx;
	}

	.group-bg {
		background: #EFEFF4;
		height: 10px;
	}

	.group-title {
		padding: 10px 20px;
	}

	.cu-form-group {
		border-bottom: 1px solid #D8D8D8;
		margin: 0 10px;
		background: #fff;

		.file {
			flex: 1;

			.item {
				display: inline-block;
				padding: 10px 20px 0 0;
				position: relative;

				.cuIcon-add {
					box-sizing: border-box;
					display: block;
					width: 60px;
					height: 60px;
					line-height: 60px;
					text-align: center;
					border: 1px dashed #DDDDDD;
					font-size: 30px;
					color: #aaa;
				}

				.cuIcon-close {
					position: absolute;
					right: 20px;
					top: 10px;
					width: 18px;
					height: 18px;
					line-height: 18px;
					border-radius: 2px;
					text-align: center;
					background-color: #00000060;
					color: #FFFFFF;
					z-index: 999;
				}

				.img {
					display: block;
					width: 60px;
					height: 60px;
				}

				.label {
					display: block;
					line-height: 25px;
					font-size: 12px;
					text-align: center;
					color: #aaa;
				}
			}

		}
	}

	.no-border {
		border: none;
	}

	.uni-list-cell-item {
		padding: 5px 0;
		margin: 5px 0;
		width: 200px;
		text-align: left;

		text {
			margin-left: 10px;
			position: relative;
			top: 2px;
		}
	}

	.moda-msg {
		text-align: center;
		font-size: 16px;
		margin: 10px 0;
	}
</style>